package com.owulia.leetcode.string;

import java.util.HashMap;
import java.util.Map;

public class CheckPermutation {
    public static boolean checkPermutation(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        Map<Character, Integer> charCount = new HashMap<>();
        for (int i = 0; i < s1.length(); i++) {
            char cur = s1.charAt(i);
            if (charCount.containsKey(cur)) {
                charCount.put(cur, charCount.get(cur) + 1);
            } else {
                charCount.put(cur, 1);
            }
        }

        for (int i = 0; i < s1.length(); i++) {
            char cur = s2.charAt(i);
            Integer count = charCount.get(cur);
            if (null == count || count == 0) {
                return false;
            } else {
                charCount.put(cur, count - 1);
            }
        }
        return true;
    }
}
